package annuaire.rmi.server.impl;

import java.rmi.AlreadyBoundException;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;

import annuaire.config.RmiConfigLoader;
import annuaire.dao.IAnnuaireDao;
import annuaire.dao.impl.JpaAnnuaireDao;
import annuaire.rmi.remotedObject.IRemotedAnnuaire;
import annuaire.rmi.remotedObject.impl.RemotedAnnuaireImpl;
import annuaire.rmi.server.IRmiServer;

/**
 * @author Yannick Lorenzati & Stephanie Malakian
 * 
 */
public class RmiServer implements IRmiServer {

	/**
	 * 
	 */
	private Registry registry = null;

	/*
	 * (non-Javadoc)
	 * 
	 * @see annuaire.rmi.server.IRmiServer#addExportedObject(java.lang.String,
	 * java.rmi.Remote)
	 */
	@Override
	public void addExportedObject(String name, Remote o) throws RemoteException {
		try {

			/** resistance au null */
			if (name == null || name.equals("") || o == null) {
				return;
			}

			registry.bind(name, o);
		} catch (AlreadyBoundException e) {
			System.err.println("name est deja utilise dans le registre"); //$NON-NLS-1$
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see annuaire.rmi.server.IRmiServer#close()
	 */
	@Override
	public void close() {

		String[] tabName = null;

		try {
			tabName = registry.list();
			for (String element : tabName) {
				delExportedObject(element);
			}

			registry = null;

			System.err.println("RmiServer.close()");
		} catch (RemoteException e) {

			e.printStackTrace();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see rmi.server.IRmiServer#delExportedObject(java.lang.String)
	 */
	@Override
	public void delExportedObject(String name) throws RemoteException {
		try {

			if (name == null || name.equals("")) {
				return;
			}

			registry.unbind(name);
		} catch (NotBoundException e) {
			System.err.println("name n'existe pas dans le registre");
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see annuaire.rmi.server.IRmiServer#init()
	 */
	@Override
	public void init(int port) {

		/** on creer un registre */
		try {
			registry = java.rmi.registry.LocateRegistry.createRegistry(port);

			// TODO un gestionnaire de securité ?

			System.out.println("serveur initialise");

		} catch (RemoteException e) {

			System.err.println("RmiServer.init()");
			e.printStackTrace();
			System.exit(1);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see annuaire.rmi.server.IRmiServer#run()
	 */
	@Override
	public void run() throws RemoteException {

		IAnnuaireDao dao = null;
		IRemotedAnnuaire annu = null;

		dao = new JpaAnnuaireDao();
		dao.init();

		annu = RemotedAnnuaireImpl.instance();
		((RemotedAnnuaireImpl) annu).setDao(dao);
		((RemotedAnnuaireImpl) annu).init();

		addExportedObject(RmiConfigLoader.getParam("nameRemotedAnnuaire"), annu);
		System.out.println("RmiServer.run()");
	}

	public static void main(String[] args) {

		IRmiServer serv = null;
		try {
			serv = new RmiServer();
			serv.init(Integer
					.parseInt(RmiConfigLoader.getParam("registryPort")));

			serv.run();
		} catch (RemoteException e) {

			e.printStackTrace();
		}
	}

}
